package com.live.task;

import java.net.URI;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.webkit.WebView;
import android.widget.Toast;

import com.live.activity.R;
import com.live.common.Constant;
import com.live.tools.LiveTools;

public class LiveVideoWatch extends AsyncTask<Bundle, String, String> {
	private WebView webView = null;

	private String htmlCode = null;

	private String htmlPre = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"></head>"
			+ "<body style='margin:0; pading:0; background-color: black;'>";

	private String htmlPost = "</body></html>";

	private Context context = null;

	public LiveVideoWatch(Context context, WebView webView) {
		this.context = context;
		this.webView = webView;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected String doInBackground(Bundle... params) {
		Log.i("PageTask", "doInBackground... " + Thread.currentThread().getId());
		String url = Constant.SITE_URL + "live_video_watch.php?locale=" + LiveTools.getLocaleLanguage() + "&videoId="
				+ params[0].getString("videoId") + "&";

		// 取得本机MAC地址
		String mac = LiveTools.getLocalMacAddress(context);
		if (mac == null) {
			mac = "android%20AVD";
		}
		// 此URL记录观看比赛者
		SharedPreferences store = context.getSharedPreferences(Constant.PREFS_NAME, 0);
		url += "&id=" + store.getString("id", null) + "&mac=" + mac;

		// 记录观看比赛详细页的信息
		setVideoWatch(url);

		publishProgress(params[0].getString("videoUrl"), params[0].getString("videoCategory"));
		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		super.onProgressUpdate(values);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());

		// 篮球（NBA）
		if ("0".equals(values[1])) {
			htmlCode = "<embed style='width:100%; height:100%' src='http://www.platipus.nl/flvplayer/download/1.0/FLVPlayer.swf?fullscreen=true&video=@VIDEO@' "
					+ "  autoplay='true' quality='high' bgcolor='#000' name='VideoPlayer' align='middle'"
					+ "  width='100%' height='100%' allowScriptAccess='*' allowFullScreen='true' type='application/x-shockwave-flash' "
					+ "  pluginspage='http://www.macromedia.com/go/getflashplayer' />" + "";

			htmlCode = htmlCode.replaceAll("@VIDEO@", values[0]);
			webView.loadDataWithBaseURL("fake://fake/fake", htmlPre + htmlCode + htmlPost, "text/html", "UTF-8", null);
		}
		// 足球
		else if ("2".equals(values[1]) || "3".equals(values[1])) {
			htmlCode = "<object id='sinaplayer' width='100%' height='100%'>"
					+ "<param name='allowScriptAccess' value='always' />"
					+ "<embed pluginspage='http://www.macromedia.com/go/getflashplayer' src='@VIDEO@'"
					+ " type='application/x-shockwave-flash' name='sinaplayer' allowFullScreen='true' allowScriptAccess='always' width='100%' height='100%'>"
					+ "</embed></object>" + "";

			htmlCode = htmlCode.replaceAll("@VIDEO@", values[0]);
			webView.loadDataWithBaseURL("fake://fake/fake", htmlPre + htmlCode + htmlPost, "text/html", "UTF-8", null);
		}
	}

	@Override
	protected void onPostExecute(String result) {
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());
		Toast toast = Toast.makeText(context, R.string.load_video, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}

	/**
	 * 记录观看视频信息
	 * 
	 * @param url
	 * @return
	 */
	private void setVideoWatch(String url) {
		HttpClient client = new DefaultHttpClient();

		// 提拱默认的HttpClient实现
		HttpPost request;
		try {
			request = new HttpPost(new URI(url));
			client.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}